
// 自动导入 view文件下的路由配置文件 router.js
function requireViewRoutes () {
    const files = require.context('@/view', true, /router\.js$/)
    return files.keys().reduce((r, p) => {
        return r.concat(files(p).default)
    }, [])
}

// keep-alive 时 路由的组件添加name属性
function initKeepAliveAttr (routes) {
    routes.forEach(item => {
        // 只有是 Main 组件包裹的路由才需要加 name
        if (
            item.children &&
            typeof item.component === 'object' &&
            item.component.name === 'Main'
        ) {
            routesAddComponentName(item.children)
        }
    })

    // 获取带有name属性的新组件
    function getAsynComponent (asyComponent, name) {
        return _ =>
            asyComponent().then(data => {
                const component = data.default
                component.name = name
                return data
            })
    }

    // 路由的 component 组件添加组件名
    function routesAddComponentName (routes) {
        routes.forEach(route => {
            const { name = null, component, children = null } = route
            if (typeof component === 'function' && name) {
                route.component = getAsynComponent(component, name)
            }
            children && routesAddComponentName(children)
        })
    }
}

/**
 * meta: {
 *  wt: 白名单
 * }
 */
let routes = [
    {
        path: '',
        name: 'index',
        meta: {
            title: '深航信eMTouch',
            wt: true
        },
        component: () => import('@/view/index')
    }
]

const viewRoutes = requireViewRoutes()
initKeepAliveAttr(viewRoutes)

routes = routes.concat(viewRoutes)

export default routes
